<?php
class RanklistAction extends Action {
	public function index() {
		$this->assign ( 'title', '羽毛球积分赛 - 积分排行' );
		
		// 判断用户登录信息
		$this->assign ( 'admin', 'none' );
		if (session ( 'username' )) {
			$this->assign ( 'loginuser', session ( 'username' ) );
			$this->assign ( 'userscore', session ( 'userscore' ) );
			$this->assign ( 'usermatchcount', session ( 'usermatchcount' ) );
			$this->assign ( 'loginusercolor', get_color ( session ( 'usergender' ) ) );
			
			if (intval ( session ( 'userlevel' ) ) < 1) {
				$this->assign ( 'userstatus', '（未认证）' );
			} else {
				$this->assign ( 'userstatus', '' );
			}
			
			if (intval ( session ( 'userlevel' ) ) > 1) {
				$this->assign ( 'admin', 'inline' );
			}
			
			$this->assign ( 'logoffblock', 'inline' );
			$this->assign ( 'loginblock', 'none' );
		} else {
			$this->assign ( 'logoffblock', 'none' );
			$this->assign ( 'loginblock', 'inline' );
		}
		
		$users = M ( 'Users' );
		
		$data = $users->where ( 'level>0 and total_match_count>0' )->order ( 'total_score desc' )->field ( 'user_id,username,gender,total_score,single_score,double_score,small_score,total_match_count,single_win_count,double_win_count,small_win_count,last_single_match_date,last_double_match_date,last_small_match_date' )->select ();
		$last_rank = 0;
		for($i = 0; $i < count ( $data ); $i ++) {
			$user_gender [$data [$i] ['username']] = $data [$i] ['gender'];
			$data [$i] ['color'] = get_color ( $data [$i] ['gender'] );
			
			$data [$i] ['last_match_date'] = $data [$i] ['last_single_match_date'];
			if ($data [$i] ['last_match_date'] < $data [$i] ['last_double_match_date']) {
				$data [$i] ['last_match_date'] = $data [$i] ['last_double_match_date'];
			}
			if ($data [$i] ['last_match_date'] < $data [$i] ['last_small_match_date']) {
				$data [$i] ['last_match_date'] = $data [$i] ['last_small_match_date'];
			}
			
			$data [$i] ['win_ratio'] = 100.0 * ($data [$i] ['single_win_count'] + $data [$i] ['double_win_count'] + $data [$i] ['small_win_count']) / $data [$i] ['total_match_count'];
			$data [$i] ['win_ratio'] = round ( $data [$i] ['win_ratio'] ) . '%';
			
			if ($i > 0 && $data [$i] ['total_score'] == $data [$i - 1] ['total_score']) {
				$data [$i] ['rank'] = $last_rank;
			} else {
				$data [$i] ['rank'] = $i + 1;
			}
			$last_rank = $data [$i] ['rank'];
		}
		$this->assign ( 'comblist', $data );
		
		$single_data = $users->where ( 'level>0 and single_match_count>0' )->order ( 'single_score desc' )->field ( 'username,gender,single_score,single_match_count,single_win_count,last_single_match_date' )->select ();
		$last_rank = 0;
		for($i = 0; $i < count ( $single_data ); $i ++) {
			$single_data [$i] ['color'] = get_color ( $single_data [$i] ['gender'] );
			
			$single_data [$i] ['win_ratio'] = 100.0 * $single_data [$i] ['single_win_count'] / $single_data [$i] ['single_match_count'];
			$single_data [$i] ['win_ratio'] = round ( $single_data [$i] ['win_ratio'] ) . '%';
			
			if ($i > 0 && $single_data [$i] ['single_score'] == $single_data [$i - 1] ['single_score']) {
				$single_data [$i] ['rank'] = $last_rank;
			} else {
				$single_data [$i] ['rank'] = $i + 1;
			}
			$last_rank = $single_data [$i] ['rank'];
		}
		$this->assign ( 'singlelist', $single_data );
		
		$double_data = $users->where ( 'level>0 and double_match_count>0' )->order ( 'double_score desc' )->field ( 'username,gender,double_score,double_match_count,double_win_count,last_double_match_date' )->select ();
		$last_rank = 0;
		for($i = 0; $i < count ( $double_data ); $i ++) {
			$double_data [$i] ['color'] = get_color ( $double_data [$i] ['gender'] );
			
			$double_data [$i] ['win_ratio'] = 100.0 * $double_data [$i] ['double_win_count'] / $double_data [$i] ['double_match_count'];
			$double_data [$i] ['win_ratio'] = round ( $double_data [$i] ['win_ratio'] ) . '%';
			
			if ($i > 0 && $double_data [$i] ['double_score'] == $double_data [$i - 1] ['double_score']) {
				$double_data [$i] ['rank'] = $last_rank;
			} else {
				$double_data [$i] ['rank'] = $i + 1;
			}
			$last_rank = $double_data [$i] ['rank'];
		}
		$this->assign ( 'doublelist', $double_data );
		
		$small_data = $users->where ( 'level>0 and small_match_count>0' )->order ( 'small_score desc' )->field ( 'username,gender,small_score,small_match_count,small_win_count,last_small_match_date' )->select ();
		$last_rank = 0;
		for($i = 0; $i < count ( $small_data ); $i ++) {
			$small_data [$i] ['color'] = get_color ( $small_data [$i] ['gender'] );
			
			$small_data [$i] ['win_ratio'] = 100.0 * $small_data [$i] ['small_win_count'] / $small_data [$i] ['small_match_count'];
			$small_data [$i] ['win_ratio'] = round ( $small_data [$i] ['win_ratio'] ) . '%';
			
			if ($i > 0 && $small_data [$i] ['small_score'] == $small_data [$i - 1] ['small_score']) {
				$small_data [$i] ['rank'] = $last_rank;
			} else {
				$small_data [$i] ['rank'] = $i + 1;
			}
			$last_rank = $small_data [$i] ['rank'];
		}
		$this->assign ( 'smalllist', $small_data );
		
		for($i = 0; $i < count ( $data ); $i ++) {
			$username [$data [$i] ['user_id']] = $data [$i] ['username'];
		}
		// dump ( $username );
		
		$matches = M ( 'MatchRecords' );
		$match_data = $matches->where ( 'match_type="double"' )->order ( 'match_date,match_id' )->select ();
		for($i = 0; $i < count ( $match_data ); $i ++) {
			if ($match_data [$i] ['pros_player1'] < $match_data [$i] ['pros_player2']) {
				$key1 = $username [$match_data [$i] ['pros_player1']] . '/' . $username [$match_data [$i] ['pros_player2']];
			} else {
				$key1 = $username [$match_data [$i] ['pros_player2']] . '/' . $username [$match_data [$i] ['pros_player1']];
			}
			$parter_scores [$key1] += $match_data [$i] ['var_score_pros_player1'] + $match_data [$i] ['var_score_pros_player2'];
			$parter_match_cnt [$key1] += 1;
			$parter_date [$key1] = $match_data [$i] ['match_date'];
			
			if ($match_data [$i] ['cons_player1'] < $match_data [$i] ['cons_player2']) {
				$key2 = $username [$match_data [$i] ['cons_player1']] . '/' . $username [$match_data [$i] ['cons_player2']];
			} else {
				$key2 = $username [$match_data [$i] ['cons_player2']] . '/' . $username [$match_data [$i] ['cons_player1']];
			}
			$parter_scores [$key2] += $match_data [$i] ['var_score_cons_player1'] + $match_data [$i] ['var_score_cons_player2'];
			$parter_match_cnt [$key2] += 1;
			$parter_date [$key2] = $match_data [$i] ['match_date'];
			
			if ($match_data [$i] ['pros_score'] > $match_data [$i] ['cons_score']) {
				$parter_win_cnt [$key1] += 1;
			} else {
				$parter_win_cnt [$key2] += 1;
			}
		}
		arsort ( $parter_scores );
		// dump ( $parter_scores );
		
		$i = 0;
		foreach ( $parter_scores as $key => $value ) {
			if ($i > 1 && $value == $parter_scores [$i - 1] ['score']) {
				$parterlist [$i] ['rank'] = $parterlist [$i - 1] ['rank'];
			} else {
				$parterlist [$i] ['rank'] = $i + 1;
			}
			$parterlist [$i] ['score'] = $value;
			$parterlist [$i] ['match_count'] = $parter_match_cnt [$key];
			$parterlist [$i] ['win_ratio'] = round ( 100.0 * $parter_win_cnt [$key] / $parter_match_cnt [$key] ) . "%";
			$parterlist [$i] ['last_match_date'] = $parter_date [$key];
			
			$pos = strpos ( $key, '/' );
			$parterlist [$i] ['partner1'] = substr ( $key, 0, $pos );
			$parterlist [$i] ['partner2'] = substr ( $key, $pos + 1 );
			$parterlist [$i] ['color1'] = get_color ( $user_gender [$parterlist [$i] ['partner1']] );
			$parterlist [$i] ['color2'] = get_color ( $user_gender [$parterlist [$i] ['partner2']] );
			
			$i ++;
			// if ($i >= 10) {
			// break;
			// }
		}
		$this->assign ( 'parterlist', $parterlist );
		
		$this->display ();
	}
}